﻿//-----------------------------------------------------------------------
// <copyright file="SampleChatService.cs" company="FastNET Services">
//     Copyright (c) 2008-2009. All Rights Reserved.
// </copyright>
// <license>
//     GPLv2
// </license>
// <warranty>
//     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
//     BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
//     DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </warranty>
//-----------------------------------------------------------------------
#region Usings

using System.Collections.Generic;
using GateShift.Services.SDK.Services;
using GateShift.Services.SDK.Services.HostIntegration;

#endregion

namespace SampleChat
{
    /// <summary>
    /// Demonstration class for the server's service system
    /// </summary>
    [LoadableService(typeof(ISampleChatService))]
    public class SampleChatService : ServiceBase, ISampleChatService, IClientDataHandler, ICommandLineInterfaceHandler
    {
        /// <summary>
        /// Initializes this service with the host
        /// </summary>
        public override void Initialize()
        {
            ServerHost.SessionHost.RegisterClientDataHandler(255, this);
            ServerHost.ServiceHost.RegisterServiceCommandHandler("chat", this);
            base.Initialize();
        }

        /// <summary>
        /// Handles data sent from the client
        /// </summary>
        /// <param name="session">IClientSession that sent the data</param>
        /// <param name="serviceCode">Packet Type Code</param>
        /// <param name="data">Binary data representation of the packet</param>
        /// <param name="wasEncrypted">Whether or not the packet was encrypted</param>
        void IClientDataHandler.HandleClientData(IClientSession session, byte serviceCode, byte[] data, bool wasEncrypted)
        {
            foreach (IClientSession client in ServerHost.SessionHost.GetSession())
            {
                client.SendData(1, serviceCode, data, false);
            }
        }

        /// <summary>
        /// Handles commands from the administrative interface
        /// </summary>
        /// <param name="arguments">List of arguments to process</param>
        /// <returns>Boolean whether or not the command was handled</returns>
        public bool HandleCommand(List<string> arguments)
        {
            string message = string.Empty;

            foreach (string item in arguments)
            {
                message += item + " ";
            }

            foreach (IClientSession client in ServerHost.SessionHost.GetSession())
            {
                client.SendData(1, 0, System.Text.Encoding.ASCII.GetBytes("SYSTEM) " + message.Trim()), false);
            }

            return true;
        }
    }
}
